<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="msapplication-window" content="width=device-width;height=device-height">
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
<title>我的鱼篓</title>
<link rel="stylesheet" href="../css/common/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="../css/myShoppingCart.css">
<link rel="stylesheet" href="../css/common/weui.min.css">
<link rel="stylesheet" href="../css/common/jquery-weui.min.css">
<link rel="stylesheet" href="../css/base.css">

<script src="../js/common/jquery-2.0.0.js"></script>
<script src="../js/common/jquery.mobile-1.4.5.js"></script>
<script src="../js/ajax_req.js"></script>
<script src="../js/api.js"></script>
		
<script src="tmpl/myShoppingCart.js"></script>

<!--
	作者：wyy8@live.cn
	时间：2017-06-06
	描述：微信
-->
<script type="application/javascript" src="../js/common/jweixin-1.0.0.js"></script>
<script type="application/javascript" src="../js/common/jquery-weui.min.js"></script>
<!--模板-->
<script type="application/javascript" src="../plugins/jquery-tmpl/jquery.tmpl.js"></script>
<script type="application/javascript" src="../plugins/jquery-tmpl/jquery-jtemplates.js"></script>

</head>
<body>
	<div data-role="page" id="mainPage">
	
		<div id="div_edit" style="margin-top: 0rem;"><span onclick="eidtStatus()" id="span-edit" class="span-edit">编辑</span></div>
		
		<hr style="width:100%;height:0.1rem;color:gray;opacity:0.2;"></hr>
		
		<div id="tmpl_div_content"></div>
		
		<div data-role="footer" data-position="fixed" class="foot-div">
			<div class="ui-checkbox checkAll">
			    <!--<input name="checkbox-enhanced" onchange="onchangeAll(this)" style="display:none;" id="checkbox-enhanced" data-enhanced="true" type="checkbox">-->
			    <img id="img_check_all" style="margin: 1px 5px;margin-top: 1rem;" width="16" height="16" src="../image/input.png" onclick="onchangeAll()">
			    <span style="position: absolute;color: #333;width: 30px;font-size: 1.2rem;top: -0.3rem;" onclick="onchangeAll()">全选</span>
			</div>
			<div class="liji_goumai" onclick="submit()">
				<span>结算</span>
			</div>
			<div style="float: right;width: auto;height: 100%;margin-right: 1rem;text-align: right;">
				<span style='font-size: 1.2rem;color: #333333;'>合计：</span>
				<span style='font-size: 1.1rem;color: red;margin-top: 0.1rem;'>¥ </span>
				<span id="priceAll" style="font-size: 1.4rem;color: red;text-align: left;width: 7.5rem;">0</span>
			</div>
			
			
		</div>
	
	</div>
	<script>
		var productList = {status:1}
		login(window.location.href)
		ajax(api.carItemList, {user_id: 1}, function(data){
			for(var i=0;i<data.length;i++){
				if(data[i].productPackage == null) {
					data[i].productPackage = {
						priceAll: 0
					}
				}
				/*if(data[i].productPackage.original_price != null && data[i].productPackage.price != null){
					data[i].productPackage.quantity =parseInt(data[i].productPackage.original_price/data[i].productPackage.price)
				}else{
					data[i].productPackage.quantity = 0
					data[i].productPackage.price = 0
				}*/
			}
			productList.list = data
			//购物车为空的情况
			if(data == null || data.length <= 0){
				$("#div_edit").remove()
				$(".foot-div").remove()
				$("#mainPage").css("position","fixed")
				$("#mainPage").css("margin-top","-50px")
			}
			$("#tmpl_div_content").setTemplateURL('tmpl/myShoppingCartTmpl.html');
			$("#tmpl_div_content").setParam("width", window.screen.width);
			$("#tmpl_div_content").setParam("height", window.screen.height); 
			$("#tmpl_div_content").processTemplate(productList);
		})
		//编辑和完成切换
		function eidtStatus(){
			//目前为不可编辑状态，点击后变成可编辑状态
			if(productList.status == 1){
				$("#span-edit").html("完成")
				$ (".liji_goumai span").html("删除")
				productList.status = 2
				$("#tmpl_div_content").setTemplateURL('tmpl/myShoppingCartTmpl.html');
				$("#tmpl_div_content").setParam("width", window.screen.width);
				$("#tmpl_div_content").setParam("height", window.screen.height); 
				$("#tmpl_div_content").processTemplate(productList);
			}else{
				//移除编辑状态选中的下架的商品
				for(var i=0;i<productList.list.length;i++){
			 		if(productList.list[i].is_valid == 0){
			 			productList.list[i].productPackage.checked = "no"
			 		}else{
			 			productList.list[i].productPackage.checked = "checked"
			 		}
			 	}
				$("#span-edit").html("编辑")
				$ (".liji_goumai span").html("结算")
				productList.status = 1
				$("#tmpl_div_content").setTemplateURL('tmpl/myShoppingCartTmpl.html');
				$("#tmpl_div_content").setParam("width", window.screen.width);
				$("#tmpl_div_content").setParam("height", window.screen.height); 
				$("#tmpl_div_content").processTemplate(productList);
				
				//点击完成更新购物车信息
				console.log('点击完成更新购物车信息')
				var sku_list = []
				for(var i=0;i<productList.list.length;i++){
					if(productList.list[i].productPackage.product_id != null){
							sku_list.push({
											product_id:productList.list[i].productPackage.product_id, 
											sku_id:productList.list[i].productPackage.id, 
											quantity:productList.list[i].quantity})
						}
				}
				console.warn(sku_list)
				//修改购物车
				ajax(api.addBatchProduct, {product_sku: JSON.stringify(sku_list)}, function(data){
					
				})
			}
		}
		
		function submit(){
			var str = "";
			var ids = ""
			for(var i=0;i<productList.list.length;i++){
				if(productList.list[i].productPackage.checked == "checked"){
					if(productList.list[i].productPackage.product_id != null){
						str += productList.list[i].productPackage.product_id+"-"+
						productList.list[i].productPackage.id + "-" +
						productList.list[i].quantity+","
					}
					if(productList.list[i].id != null){
						ids += productList.list[i].id + ","
					}
				}
			}
			
			if(productList.status == 1){
				if(str == ""){
					toast("请选择产品！",2)
					return
				}
				window.location.href="confrimOrder.html?sku="+str
			}else{
				//删除购物车
				if(ids == ""){
					toast("请选择产品！",2)
					return
				}
				
				confirm("确认删除吗？", "确认删除?", function() {
		           var idd = ids.split(',')
					console.log(ids)
					ids = ids.substring(0, ids.length-1)
					console.log(ids)
					ajax(api.deleteProduct, {ids:ids}, function(data){
						toast("删除成功！")
						for(var j=0;j<idd[j].length;j++){
							for(var m=0;m<productList.list.length;m++){
								if(productList.list[m].id == idd[j]){
									productList.list.splice(m,1);
								}
							}
						}
						
						$("#tmpl_div_content").setTemplateURL('tmpl/myShoppingCartTmpl.html');
						$("#tmpl_div_content").setParam("width", window.screen.width);
						$("#tmpl_div_content").setParam("height", window.screen.height); 
						$("#tmpl_div_content").processTemplate(productList);
						//如果购物车为空，那么强制刷新
						if(productList.list.length<=0){
							$("#div_edit").remove()
							$(".foot-div").remove()
						}
						
					})
		        });
			}
		}
		
		
	</script>
</body>
</html>